package cqie.cn.frame.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import cqie.cn.frame.entity.SysUser;
import cqie.cn.frame.service.ISysUserService;
import cqie.cn.frame.utils.AssertUtil;
import cqie.cn.frame.utils.MD5Utils;
import cqie.cn.frame.utils.ResultVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author 王林
 * @version 1.0
 * @date 2021-09-23 周四
 * @time 9:30
 * @description
 */
@Api(tags = "公共管理模块", value = "登录登出重置密码")
@ApiModel(value = "公共管理模块")
@RequestMapping("/frame/common")
@RestController
public class LoginController {

    @Resource
    private ISysUserService userService;

    //登录
    @ApiOperation(value = "登录", notes = "")
    @PostMapping(value = "/login")
    public ResultVo login(@RequestBody SysUser mo) {
        try {
            QueryWrapper<SysUser> wrapper = new QueryWrapper<>();
            wrapper.eq("account", mo.getAccount());
            List<SysUser> userList = userService.list(wrapper);
            //验证账号
            if (userList.size() == 0) {
                return ResultVo.error("账号错误");
            }
            boolean passwordRight = false;
            boolean statusRight = false;
            for (SysUser sysUser : userList) {
                if (sysUser.getPassword().equals(MD5Utils.EncodeMD5(mo.getPassword()))) {
                    passwordRight = true;
                    if (sysUser.getStatus() == 0) {
                        statusRight = true;
                        //采用 session 保存用户，根据上下文获取session
                        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
                        HttpSession session = request.getSession();
//                        若存在，则修改；若不存在，则存入
                        session.setAttribute("loginUser", sysUser);
                        break;
                    }
                }
            }
            if (passwordRight == false) {
                return ResultVo.error("登录密码错误");
            }
            if (statusRight == false) {
                return ResultVo.error("用户已禁用");
            }
            return ResultVo.success("登录成功");
        } catch (Exception e) {
            return ResultVo.error(e.getMessage());
        }
    }

    //注销登录
    @ApiOperation(value = "注销登录", notes = "")
    @PostMapping(value = "/logout")
    public ResultVo logout() {
        try {
            HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            HttpSession session = request.getSession();
            session.removeAttribute("loginUser");  //不用若为空，不会影响执行。
//        session.setAttribute("loginUser",null);    也可以设为空，但判断时要
            return ResultVo.success("注销成功！");

        } catch (Exception e) {
            return ResultVo.success("注销失败！");
        }
    }

    //重置密码
    @ApiOperation(value = "重置密码", notes = "")
    @PutMapping(value = "/resetPassword")
    public ResultVo resetPassword(@RequestParam Long id) {
        String defaultPassword = "123456";
        SysUser mo = userService.getById(id);

        AssertUtil.isNotNull(mo, "该用户不存在！！！");
        mo.setPassword(MD5Utils.EncodeMD5(defaultPassword));
        userService.updateById(mo);
        return ResultVo.success("重置密码成功！");
    }

}
